package com.zx.controller;

import com.zx.common.ResponseCode;
import com.zx.entity.Category;
import com.zx.entity.Food;
import com.zx.entity.User;
import com.zx.repository.CategoryRepository;
import com.zx.repository.FoodRepository;
import com.zx.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/food")
public class FoodController {
    @Autowired
    private FoodRepository repository;
    @Autowired
    private CategoryRepository categoryRepository;
    @Autowired
    private UserRepository userRepository;

    //添加
    @GetMapping("/addFood")
    public ModelAndView addFood(Map<String,Object> map,
                          HttpSession session){
        if (LoginStatus(map, session)) return new ModelAndView("common/error", map);
        return new ModelAndView("food/addFood");
    }
    //保存
    @PostMapping("saveFood")
    public ModelAndView saveFood(Food food,
                                 Map<String,Object> map){
        repository.save(food);
        map.put("msg",ResponseCode.SAVE_SUCCESS.getDesc());
        map.put("url","/foodshare/food/list");
        return new ModelAndView("common/success",map);
    }
    //删除美食
    @GetMapping("/delete/{foodId}")
    public ModelAndView delete(@PathVariable("foodId") Integer id,
                               Map<String,Object> map){
        Food food = repository.getOne(id);
        repository.delete(food);
        map.put("msg",ResponseCode.DELETE_SUCCESS.getDesc());
        map.put("url","/foodshare/food/list");
        return new ModelAndView("common/success",map);
    }
    //分页查看美食
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "0") Integer page,
                             @RequestParam(value = "size", defaultValue = "2") Integer size,
                             Map<String,Object> map){
        PageRequest request = PageRequest.of(page,size);
        Page<Food> food = repository.findAll(request);
        map.put("food",food);
        return new ModelAndView("food/list",map);
    }
    //编辑美食
    @GetMapping("/list/{foodId}")
    public ModelAndView EditUser(@PathVariable("foodId") Integer id,
                                 Map<String,Object> map,
                                 HttpSession session){
        if (LoginStatus(map, session)) return new ModelAndView("common/error", map);
        Food food = repository.getOne(id);
        map.put("food",food);
        return new ModelAndView("food/editFood",map);
    }
    //判断登录状态，获取登录信息
    private boolean LoginStatus(Map<String, Object> map, HttpSession session) {
        String username = (String) session.getAttribute("loginUser");
        if(StringUtils.isEmpty(username)){
            map.put("msg", ResponseCode.LOGIN_ERROR.getDesc());
            map.put("url","/foodshare/user/login");
            return true;
        }
        User user = userRepository.findByUsername(username);
        map.put("user",user);
        List<Category> categoryList = categoryRepository.findAll();
        map.put("category",categoryList);
        return false;
    }
}
